/***************************************************
> Copyright (C) 2024 ==KINGYI== All rights reserved.
> File Name: root_menu.c
> Author:MOKI
> Mail:1913964829@qq.com
> Created Time: 2024年09月13日 星期五 18时21分21秒
***************************************************/
#include "global.h"
// 管理员账号功能
void rootManage()
{
    int choice, quit_flag = 0;
    while (1)
    {
        printf("***************************************\n");
        printf("管理员菜单：\n");
        printf("1.管理员工信息\n");
        printf("2.管理植物信息\n");
        printf("3.管理游客信息\n");
        printf("4.管理监控信息\n");
        printf("5.管理环境信息\n");
        printf("0.返回上一级\n");
        printf("输入选择：");
        setbuf(stdin, NULL);
        scanf("%d", &choice);
        switch (choice)
        {
        case 1:
            manageEmployee();
            break;
        case 2:
            managePlants();
            break;
        case 3:
            manageVisitor();
            break;
        case 4:
            manageSurveillanceData();
            break;
        case 5:
            manageEnvironmentData();
            break;
        case 0:
            quit_flag = 1;
            break;
        default:
            printf("输入错误重新输入\n");
            break;
        }
        if (quit_flag == 1)
        {
            break;
        }
    }
}
void manageEmployee()
{
    int choice = -1, quit_flag = 0;
    char ch = 0;
    DataList *head_E = NULL;
    head_E = createEmployeeList();
    int listlen;
    while (1)
    {
        printf("***************************************\n");
        printf("请选择你想要进行的操作：\n");
        printf("1.查看员工信息\n");
        printf("2.修改员工工资\n");
        printf("3.删除员工\n");
        printf("0.返回上一级\n");
        setbuf(stdin, NULL);
        scanf("%d", &choice);
        switch (choice)
        {
        case 1:
            displayList_E(head_E);
            break;
        case 2:
            // 修改员工工资
            char name[50] = {0};
            double salary = 0;
            int alter_flag = 0;
            while (1)
            {
                printf("请输入员工姓名：");
                setbuf(stdin, NULL);
                scanf("%s", name);
                printf("请输入修改后的工资：");
                setbuf(stdin, NULL);
                scanf("%lf", &salary);
                alter_flag = alterSalary(head_E, name, salary);
                if (alter_flag == 1)
                {
                    printf("修改成功\n");
                    alter_flag = 0;
                }
                else
                {
                    printf("修改失败\n");
                }
                printf("是否继续修改（y/n）?");
                setbuf(stdin, NULL);
                scanf("%c", &ch);
                if (ch == 'n')
                {
                    ch = 0;
                    break;
                }
            }
            break;
        case 3:
            // 删除第几个员工
            int n = 0;
            while (1)
            {
                printf("输入要删除的员工序号：");
                setbuf(stdin, NULL);
                scanf("%d", &n);
                listlen = listLen(head_E);
                if (n > 0 && n <= listlen)
                {
                    head_E = deletenList_E(head_E, n);
                    printf("删除成功\n");
                    displayList_E(head_E);
                }
                else
                {
                    printf("输入超出范围(输入n退出)\n");
                    setbuf(stdin, NULL);
                    scanf("%c", &ch);
                    if (ch == 'n')
                    {
                        ch = 0;
                        break;
                    }
                    continue;
                }
                break;
            }
            break;
        case 0:
            quit_flag = 1;
            break;
        default:
            printf("输入错误重新输入\n");
            break;
        }
        if (quit_flag == 1)
        {
            break;
        }
    }
    fileWriteEmployee(head_E);
    freeList(head_E);
}
void displayList_E(DataList *head)
{
    DataList *end = NULL;
    int i = 0;
    end = head;
    while (end != NULL)
    {
        i++;
        printf("%d\t姓名：%s\t职位：%s\t联系方式：%s\t薪资：%.2f\n", i, end->datatype.employee.name,
               end->datatype.employee.position, end->datatype.employee.contact, end->datatype.employee.salary);
        end = end->pn;
    }
}
int alterSalary(DataList *head, char *name, double salary)
{
    DataList *end = head;
    while (end != NULL)
    {
        if (strcmp(end->datatype.employee.name, name) == 0)
        {
            end->datatype.employee.salary = salary;
            return 1;
        }
        end = end->pn;
    }
    return 0;
}
DataList *deletenList_E(DataList *head, int n)
{
    DataList *end = head, *before = head;
    if (n == 1)
    {
        end = end->pn;
        free(head);
        head = end;
        return head;
    }
    end = end->pn;
    int count = 1;
    while (end != NULL)
    {
        count++;
        if (count == n)
        {
            before->pn = end->pn;
            free(end);
            return head;
        }
        before = end;
        end = end->pn;
    }
    return NULL;
}
void manageVisitor()
{
    int choice = -1, quit_flag = 0;
    char ch = 0;
    DataList *head_V = NULL;
    head_V = createVisitorList();
    int listlen;
    while (1)
    {
        printf("***************************************\n");
        printf("请选择你想要进行的操作：\n");
        printf("1.查看游客信息\n");
        printf("2.修改游客消费记录\n");
        printf("0.返回上一级\n");
        setbuf(stdin, NULL);
        scanf("%d", &choice);
        switch (choice)
        {
        case 1:
            displayList_V(head_V);
            break;
        case 2:
            // 修改游客消费记录
            char name[50] = {0};
            double consumption = 0;
            int alter_flag = 0;
            while (1)
            {
                printf("请输入游客姓名：");
                setbuf(stdin, NULL);
                scanf("%s", name);
                printf("请输入消费：");
                setbuf(stdin, NULL);
                scanf("%lf", &consumption);
                alter_flag = alterConsumption(head_V, name, consumption);
                if (alter_flag == 1)
                {
                    printf("修改成功\n");
                    alter_flag = 0;
                }
                else
                {
                    printf("修改失败\n");
                }
                printf("是否继续修改（y/n）?");
                setbuf(stdin, NULL);
                scanf("%c", &ch);
                if (ch == 'n')
                {
                    ch = 0;
                    break;
                }
            }
            break;
        case 0:
            quit_flag = 1;
            break;
        default:
            printf("输入错误重新输入\n");
            break;
        }
        if (quit_flag == 1)
        {
            break;
        }
    }
    fileWriteVisitor(head_V);
    freeList(head_V);
}
int alterConsumption(DataList *head, char *name, double consumption)
{
    DataList *end = head;
    while (end != NULL)
    {
        if (strcmp(end->datatype.visitor.name, name) == 0)
        {
            end->datatype.visitor.consumption += consumption;
            return 1;
        }
        end = end->pn;
    }
    return 0;
}
void displayList_V(DataList *head)
{
    DataList *end = NULL;
    int i = 0;
    end = head;
    while (end != NULL)
    {
        i++;
        printf("%d\t姓名：%s\t联系方式：%s\t入园时间：%d\t消费记录：%.2f\n", i, end->datatype.visitor.name,
               end->datatype.visitor.contact, end->datatype.visitor.entry_time, end->datatype.visitor.consumption);
        end = end->pn;
    }
}
void managePlants()
{
    int choice = -1, quit_flag = 0;
    DataList *head_P = createPlantList();
    int listlen = 0;
    char ch;
    while (1)
    {
        printf("***************************************\n");
        printf("管理植物菜单：\n");
        printf("1.查看现有植物\n");
        printf("2.录入植物信息\n");
        printf("3.删除植物\n");
        printf("0.返回上一级\n");
        setbuf(stdin, NULL);
        scanf("%d", &choice);
        switch (choice)
        {
        case 1:
            displayList_P(head_P);
            break;
        case 2:
            addPlantToList(head_P);
            printf("添加成功\n");
            break;
        case 3:
            // 删除第n个植物
            int n = 0;
            while (1)
            {
                printf("输入要删除的植物序号：");
                setbuf(stdin, NULL);
                scanf("%d", &n);
                listlen = listLen(head_P);
                if (n > 0 && n <= listlen)
                {
                    head_P = deletenList_P(head_P, n);
                    printf("删除成功\n");
                    displayList_P(head_P);
                }
                else
                {
                    printf("输入超出范围(输入n退出)\n");
                    setbuf(stdin, NULL);
                    scanf("%c", &ch);
                    if (ch == 'n')
                    {
                        ch = 0;
                        break;
                    }
                    continue;
                }
                break;
            }
            // displayList_P(head_P);
            break;
        case 0:
            quit_flag = 1;
            break;
        default:
            printf("输入有误请重新输入\n");
            break;
        }
        if (quit_flag == 1)
        {
            break;
        }
    }
    fileWritePlant(head_P);
    freeList(head_P);
}
void displayList_P(DataList *head)
{
    DataList *end = NULL;
    int i = 0;
    end = head;
    while (end != NULL)
    {
        i++;
        printf("%d\t植物名称：%s\t植物种类:%s\t种植日期%d\t生长状态:%s\t病虫害情况:%s\n", i, end->datatype.plant.name,
               end->datatype.plant.species, end->datatype.plant.planting_date, end->datatype.plant.growth_status, end->datatype.plant.pest_disease);
        end = end->pn;
    }
}
void addPlantToList(DataList *head)
{
    DataList *end = head, *node = NULL;
    while (end->pn != NULL)
    {
        end = end->pn;
    }
    Plant plant;
    printf("输入植物姓名：");
    setbuf(stdin, NULL);
    scanf("%s", plant.name);
    printf("输入植物种类：");
    setbuf(stdin, NULL);
    scanf("%s", plant.species);
    printf("输入植物种植日期：");
    setbuf(stdin, NULL);
    scanf("%d", &plant.planting_date);
    printf("输入植物生长状况：");
    setbuf(stdin, NULL);
    scanf("%s", plant.growth_status);
    printf("输入植物病虫害情况：");
    setbuf(stdin, NULL);
    scanf("%s", plant.pest_disease);
    node = (DataList *)malloc(sizeof(DataList));
    memset(node, 0, sizeof(DataList));
    node->datatype.plant = plant;
    end->pn = node;
    node->pn = NULL;
}
DataList *deletenList_P(DataList *head, int n)
{
    DataList *end = head, *before = head;
    if (n == 1)
    {
        end = end->pn;
        free(head);
        head = end;
        return head;
    }
    end = end->pn;
    int count = 1;
    while (end != NULL)
    {
        count++;
        if (count == n)
        {
            before->pn = end->pn;
            free(end);
            return head;
        }
        before = end;
        end = end->pn;
    }
    return NULL;
}
void manageSurveillanceData()
{
    int choice = -1, quit_flag = 0;
    DataList *head_S = createSurveillanceDataList();
    int listlen = 0;
    char ch;
    while (1)
    {
        printf("***************************************\n");
        printf("管理监控信息菜单：\n");
        printf("1.查看监控信息\n");
        printf("2.录入监控信息\n");
        printf("3.输入时间戳查找监控\n");
        printf("0.返回上一级\n");
        setbuf(stdin, NULL);
        scanf("%d", &choice);
        switch (choice)
        {
        case 1:
            displayList_S(head_S);
            break;
        case 2:
            addSurveillanceDataToList(head_S);
            printf("添加成功\n");
            break;
        case 3:
            // 查找对应时间戳监控信息
            int timestamp = 0;
            while (1)
            {
                printf("输入时间戳：");
                setbuf(stdin, NULL);
                scanf("%d", &timestamp);
                listlen = listLen(head_S);
                findTimestamp(head_S, timestamp); // 打印对应时间戳信息
                printf("是否重新查找(y/n)?\n");
                setbuf(stdin, NULL);
                scanf("%c", &ch);
                if (ch == 'y')
                {
                    continue;
                }
                break;
            }
            break;
        case 0:
            quit_flag = 1;
            break;
        default:
            printf("输入有误请重新输入\n");
            break;
        }
        if (quit_flag == 1)
        {
            break;
        }
    }
    fileWriteSurveillanceData(head_S);
    freeList(head_S);
}
void displayList_S(DataList *head)
{
    DataList *end = NULL;
    int i = 0;
    end = head;
    while (end != NULL)
    {
        i++;
        printf("%d\t监控位置：%s\t事件类型:%s\t日期%d\n", i, end->datatype.surveillanceData.location,
               end->datatype.surveillanceData.event, end->datatype.surveillanceData.timestamp);
        end = end->pn;
    }
}
void addSurveillanceDataToList(DataList *head)
{
    DataList *end = head, *node = NULL;
    while (end->pn != NULL)
    {
        end = end->pn;
    }
    SurveillanceData surveillancedata;
    printf("输入监控地点：");
    setbuf(stdin, NULL);
    scanf("%s", surveillancedata.location);
    printf("输入监控事件：");
    setbuf(stdin, NULL);
    scanf("%s", surveillancedata.event);
    printf("输入监控时间戳：");
    setbuf(stdin, NULL);
    scanf("%d", &surveillancedata.timestamp);
    node = (DataList *)malloc(sizeof(DataList));
    memset(node, 0, sizeof(DataList));
    node->datatype.surveillanceData = surveillancedata;
    end->pn = node;
    node->pn = NULL;
}
void findTimestamp(DataList *head, int timestamp)
{
    DataList *end = head;
    int print_flag = 0;
    while (end != NULL)
    {
        if (timestamp == end->datatype.surveillanceData.timestamp)
        {
            print_flag = 1;
            printf("*\t监控位置：%s\t事件类型:%s\t日期%d\n", end->datatype.surveillanceData.location,
                   end->datatype.surveillanceData.event, end->datatype.surveillanceData.timestamp);
        }
        end = end->pn;
    }
    if (print_flag == 0)
    {
        printf("未找到\n");
    }
}
void manageEnvironmentData()
{
    int choice = -1, quit_flag = 0;
    DataList *head_Envir = createEnvironmentDataList();
    int listlen = 0;
    char ch;
    while (1)
    {
        printf("***************************************\n");
        printf("管理环境信息信息菜单：\n");
        printf("1.查看历史环境信息\n");
        printf("2.更新环境信息\n");
        printf("0.返回上一级\n");
        setbuf(stdin, NULL);
        scanf("%d", &choice);
        switch (choice)
        {
        case 1:
            displayList_Envir(head_Envir);
            break;
        case 2:
            printf("添加如下信息：\n");
            updataEnvironmentDataToList(head_Envir);
            printf("添加成功\n");
            break;
        case 0:
            quit_flag = 1;
            break;
        default:
            printf("输入有误请重新输入\n");
            break;
        }
        if (quit_flag == 1)
        {
            break;
        }
    }
    fileWriteEnvironmentData(head_Envir);
    freeList(head_Envir);
}
void displayList_Envir(DataList *head)
{
    DataList *end = NULL;
    int i = 0;
    end = head;
    while (end != NULL)
    {
        i++;
        printf("%d\t温度:%d\t湿度:%d\t光照:%d\t空气质量:%d\n", i, end->datatype.environmentData.temperature,
               end->datatype.environmentData.humidity, end->datatype.environmentData.light,
               end->datatype.environmentData.air_quality);
        end = end->pn;
    }
}
void updataEnvironmentDataToList(DataList *head)
{
    DataList *end = head, *node = NULL;
    while (end->pn != NULL)
    {
        end = end->pn;
    }
    node = (DataList *)malloc(sizeof(DataList));
    memset(node, 0, sizeof(DataList));
    node->datatype.environmentData = randEnvirData();
    node->pn = NULL;
    end->pn = node;
    printf("\t温度:%d\t湿度:%d\t光照:%d\t空气质量:%d\n", end->datatype.environmentData.temperature,
           end->datatype.environmentData.humidity, end->datatype.environmentData.light,
           end->datatype.environmentData.air_quality);
}